<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="cache-control" content="no-cache">
<meta charset="utf-8">

<head>
<title>Junior</title>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cokie.min.js"></script>
<script src="../js/qiniu.min.js"></script>
<script src="../js/common.js?v=27"></script>
<script>
    var uploadParam = {}
    var workId = urlParams('workId')
    var unitId = urlParams('unitId')
    var departId = urlParams('departId') //班级ID
    var additionalId = urlParams('additionalId') //附加作业ID
    var scene = urlParams("scene") || 'create' //来源
    var access_token = getUserToken()
    var userInfo = getUserInfo();
    var qn_token = getQiniuToken();
    setInterval(function () {
      qn_token = getQiniuToken();
    }, 600 * 1000)


    var observer = {
      next(res) { },
      error(err) {
        console.log(1, err)
      },
      complete(res) {
        console.log(res)
        if (res.key.includes('png')) {
          uploadParam.coverKey = uploadFile(res.key, '学生作业-封面', res.key,2)
        } else {
          uploadParam.projectKey = uploadFile(res.key, '学生作业-sjr', res.key,2)
        }
        uploadWork()
      }
    }

    function handleFileUploaded(res){
      console.log(res);
      if(res.success){
        var key = res.message
        if(key.endsWith('.png')){
          uploadParam.coverKey = uploadFile(key, '学生作业-封面', key,1)
        }else{
          uploadParam.projectKey = uploadFile(key, '学生作业-sb3', key,1)
        }
        uploadWork()
      }else{
        alert("上传失败：" + res.message)
      }
    }


    window.scratchJrPage = 'editor';
    window.JrConfig = {
        onSaveCloud: function(contents, name){
            console.log("content:" + contents);
            console.log(name);
            var uuid = window.uuid()
            uploadParam.projectTitle = name
            var defaultUploadType = getSysConfig('uploadType')
            if(defaultUploadType == 'qiniu'){
              upload2Qiniu(contents, 'scratchjr/' + uuid + '.sjr', name, observer)
            }else{
              update2Local(contents,name + ".sjr", 'scratchjr', handleFileUploaded)
            }
            
            window.ScratchJr.getProjectCover((r)=>{
                blob = dataURLtoBlob(r)
                if(defaultUploadType == 'qiniu'){
                  upload2Qiniu(blob, 'scratchjr/' + uuid + '.png', name, observer)
                }else{
                  update2Local(blob,name + ".png", 'scratchjr', handleFileUploaded)
                }
            })
            
        }
    }

    //上传作业
    function uploadWork() {
      if (!uploadParam.projectKey || !uploadParam.coverKey) {
        return
      }
      $.ajax({
        url: '/api/teaching/teachingWork/submit',
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        beforeSend: function (request) {
          request.setRequestHeader('X-Access-Token', access_token)
        },
        data: JSON.stringify({
          courseId: unitId,
          workCover: uploadParam.coverKey,
          workFile: uploadParam.projectKey,
          workName: uploadParam.projectTitle,
          id: workId,
          workType: 3,
          workStatus: 1,
          departId: departId,
          additionalId: additionalId,
          workScene: scene,
        }),
        success: function (res) {
          canShare = true
          if (res.code == 200) {
            alert("上传成功")
          } else {
          }
        },
        error: function () {
          
        },
        complete: function () {
        }
      })
    }

</script>
<script type="text/javascript" src="app.bundle.js?v=1"></script>
</head>
<body style="background: white;">
<div class="frame" id="frame"></div>
<div class="libframe" id="libframe"></div>
<div class="paintframe" id="paintframe"></div>

</body>
</html>
